/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.ChargeCheckItemBean;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.NaizhuAILog;

import java.util.ArrayList;
import java.util.List;

public class TargetCreateChosenChargeUserAdapterHolder extends RecyclerView.Adapter<TargetCreateChosenChargeUserAdapterHolder.ChargeUserHolder> {


    private Context mContext;
    private OnItemClickListener mItemClickListener;
    private List<ChargeCheckItemBean<PersonUserBean>> chargedUsers;

    public TargetCreateChosenChargeUserAdapterHolder(Context context, List<ChargeCheckItemBean<PersonUserBean>> chargedUsers) {
        this.mContext = context;
        this.chargedUsers = chargedUsers;
    }

    @NonNull
    @Override
    public ChargeUserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_business_target_create_charge_layout, parent, false);
        return new ChargeUserHolder(sView);
    }


    //targetBean
    @Override
    public void onBindViewHolder(ChargeUserHolder holder, int position) {
        if (!chargedUsers.get(position).isCheck()) {
            holder.chargedIcon.setVisibility(View.INVISIBLE);
        } else {
            holder.chargedIcon.setVisibility(View.VISIBLE);
        }
        PersonUserBean user = chargedUsers.get(position).getPersonUser();
        if (!TextUtils.isEmpty(user.getNickName()))
            holder.chargedName.setText(user.getNickName());
        else if (!TextUtils.isEmpty(user.getShowName()))
            holder.chargedName.setText(user.getShowName());
        NaizhuAILog.e("user.getUserLogoUrl()=" + user.getUserLogoUrl());
        if (user.getUserLogoUrl() != null)
            ImageUtils.glideThumbViewByUrl(mContext,holder.chargedThumb,user.getUserLogoUrl());
    }

    private RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            // todo log exception
            NaizhuAILog.e("Exception e" + e.getMessage());
            // important to return false so the error placeholder can be placed
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    };

    @Override
    public int getItemCount() {
        return chargedUsers.size();
    }


    public class ChargeUserHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CircleImageView chargedThumb;
        TextView chargedName;
        ImageView chargedIcon;

        public ChargeUserHolder(View view) {
            super(view);
            chargedThumb = (CircleImageView) view.findViewById(R.id.charged_thumb);
            chargedName = (TextView) view.findViewById(R.id.charged_name);
            chargedIcon = (ImageView) view.findViewById(R.id.check_icon);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int index = getAdapterPosition();
            mItemClickListener.onItemClick(v, index);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
}
